package top.jamewen.framework.sbmp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.jamewen.framework.sbmp.common.api.CommonPage;
import top.jamewen.framework.sbmp.common.api.CommonResult;
import top.jamewen.framework.sbmp.common.model.UmsMenu;
import top.jamewen.framework.sbmp.common.model.UmsResource;
import top.jamewen.framework.sbmp.common.model.UmsRole;
import top.jamewen.framework.sbmp.service.UmsMenuService;
import top.jamewen.framework.sbmp.service.UmsResourceService;
import top.jamewen.framework.sbmp.service.UmsRoleService;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author jamewen
 * @since 2021-01-12
 */
@Slf4j
@Api(tags = "后台角色管理模块")
@RestController
@RequestMapping("/role")
public class UmsRoleController {
    @Autowired
    private UmsRoleService roleService;
    @Autowired
    private UmsMenuService menuService;
    @Autowired
    private UmsResourceService resourceService;

    @ApiOperation("添加角色")
    @RequestMapping(method = RequestMethod.POST)
    public CommonResult<String> create(@RequestBody UmsRole role) {
        boolean success = roleService.create(role);
        if (success) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    @ApiOperation("修改角色")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public CommonResult<String> update(@PathVariable Long id, @RequestBody UmsRole role) {
        role.setId(id);
        boolean success = roleService.updateById(role);
        if (success) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @ApiOperation("批量删除角色")
    @RequestMapping(method = RequestMethod.DELETE)
    public CommonResult<String> delete(@RequestParam("ids") List<Long> ids) {
        boolean success = roleService.delete(ids);
        if (success) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }


    @ApiOperation("获取所有角色")
    @RequestMapping(method = RequestMethod.GET)
    public CommonResult<List<UmsRole>> listAll() {
        List<UmsRole> roleList = roleService.list();
        return CommonResult.success(roleList);
    }

    @ApiOperation("根据角色名称分页获取角色列表")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsRole>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<UmsRole> roleList = roleService.list(keyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(roleList));
    }

    @ApiOperation("修改角色状态")
    @RequestMapping(value = "/{roleId}/status", method = RequestMethod.PUT)
    public CommonResult<String> updateStatus(@PathVariable Long roleId, @RequestParam(value = "status") Integer status) {
        UmsRole umsRole = new UmsRole();
        umsRole.setId(roleId);
        umsRole.setStatus(status);
        boolean success = roleService.updateById(umsRole);
        if (success) {
            return CommonResult.success("修改角色状态成功");
        }
        return CommonResult.failed("修改角色状态失败");
    }

    @ApiOperation("获取角色相关菜单")
    @RequestMapping(value = "/{roleId}/menu", method = RequestMethod.GET)
    public CommonResult<List<UmsMenu>> listMenu(@PathVariable Long roleId) {
        List<UmsMenu> roleList = menuService.listMenu(roleId);
        return CommonResult.success(roleList);
    }

    @ApiOperation("获取角色相关资源")
    @RequestMapping(value = "/{roleId}/resource", method = RequestMethod.GET)
    public CommonResult<List<UmsResource>> listResource(@PathVariable Long roleId) {
        List<UmsResource> roleList = resourceService.listResource(roleId);
        return CommonResult.success(roleList);
    }

    @ApiOperation("给角色分配菜单")
    @RequestMapping(value = "/allocMenu", method = RequestMethod.POST)
    public CommonResult<String> allocMenu(@RequestParam Long roleId, @RequestParam List<Long> menuIds) {
        return CommonResult.success(roleService.allocMenu(roleId, menuIds));
    }

    @ApiOperation("给角色分配资源")
    @RequestMapping(value = "/allocResource", method = RequestMethod.POST)
    public CommonResult<String> allocResource(@RequestParam Long roleId, @RequestParam List<Long> resourceIds) {
        return CommonResult.success(roleService.allocResource(roleId, resourceIds));
    }

    @ApiOperation("给用户分配角色")
    @RequestMapping(value = "/allotment", method = RequestMethod.POST)
    public CommonResult<String> allotment(@RequestParam("adminId") Long adminId, @RequestParam("roleIds") List<Long> roleIds) {
        try{
            return CommonResult.success(roleService.allotment(adminId, roleIds));
        }catch (Exception e){
            log.error("权限分配失败:{}",e.getMessage());
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("获取指定用户的角色")
    @RequestMapping(value = "/{adminId}", method = RequestMethod.GET)
    public CommonResult<List<UmsRole>> getRoleList(@PathVariable Long adminId) {
        List<UmsRole> roleList = roleService.getRoleList(adminId);
        return CommonResult.success(roleList);
    }

}

